<?php

namespace App\Http\Controllers;

use App\Models\UserGroup;
use App\Models\XXlBaseForm;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class TTTestFormController extends Controller
{
    public function store(Request $request)
    {
        $data = $request->only(['name', 'phone', 'channel', 'question_data', 'url', 'group_id', 'ip', 'city']);
        if (!$data['city']) {
            if ($ip = $request->getClientIp()) {
                $ipData = geoip($ip)->toArray();
                $data += [
                    'ip' => $ip,
                    'city' => data_get($ipData, 'city'),
                    'state_name' => data_get($ipData, 'state_name')
                ];
            }
        }
        XXlBaseForm::create($data);

        return response()->json([
            'status' => 1,
            'msg' => '创建成功'
        ]);
    }

    public function storeLaike(Request $request)
    {
        $data = $request->only(['name', 'phone', 'channel', 'question_data', 'group_id', 'leads_id', 'created_at']);

        $phone = $data['phone'];
        $leadsId = $data['leads_id'];
        if (!$phone || !$leadsId)
            return response()->json([
                'status' => 1,
                'msg' => '参数错误'
            ]);

        XXlBaseForm::updateOrCreate([
            'phone' => $phone,
            'leads_id' => $leadsId,
        ], $data);

        return response()->json([
            'status' => 0,
            'msg' => '创建成功'
        ]);

    }

    public function test(Request $request)
    {
        $data = ['a' => 1];
        $ip = $request->getClientIp();
        if ($ip) {
            $ipData = geoip($ip)->toArray();
            $data += [
                'ip' => $ip,
                'city' => data_get($ipData, 'city'),
                'state_name' => data_get($ipData, 'state_name')
            ];
        }
        dd($data);

        $user = UserGroup::find(1);
        $re = $user->dispatchOnlineUser();
        if ($re) {
            $re->userGetOne($re['cache_key']);
        }
        dd($re->toArray());
    }


    public function parseReadBookData($data)
    {
        $result = [];
        foreach ($data as $item) {
            $result[$item['label']] = $item['value'];
        }

        return $result;
    }

    public function redBook(Request $request)
    {
        $d = $request->all();
        $token = $request->get('t');
        $groupId = $request->get('g', 1);
        $ctx = $request->getContent();
        $jsonData = json_decode($ctx, true);
        $data = data_get($jsonData, 'data');
        $leadsId = data_get($data, 'leads_id');
        if ($leadsId) {
            $formData = $this->parseReadBookData(data_get($data, 'data'));
            $phone = data_get($formData, '手机号');
            $name = data_get($formData, '姓名');
            $date = Carbon::parse(data_get($jsonData, 'timestamp'))->toDateTimeString();
            XXlBaseForm::updateOrCreate([
                'leads_id' => $leadsId,
            ], [
                'name' => $name,
                'phone' => $phone,
                'group_id' => $groupId,
                'channel' => '小红书',
                'question_data' => $formData,
                'created_at' => $date,
            ]);
            return response()->json([
                'ok' => 1,
            ]);

        }

        return response()->json([
            'ok' => 0
        ])->setStatusCode(201);
    }

}
